﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace pigs
{
    public class Explosion
    {
        Texture2D texture;
        Vector2 position;

        int frameCount = 0; // Current frame
        int frameSkipX = 100; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;
        Rectangle rectangle;
        public Boolean active;
        float scale; // The size of the explosion

        /// <summary>
        /// Constructor for an explosion
        /// </summary>
        /// <param name="texture"> The image to use
        /// <param name="position"> Where to center the image
        /// <param name="size"> How bit the explosion should be
        public Explosion(Texture2D texture, Vector2 position, float size)
        {

            this.active = true;
            this.texture = texture;
            this.position = position;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 3;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);

            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);

            this.scale = size;
        }

        public void Update()
        {
            // Simply update the animation when update is called
            this.animationCount++;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
            }

            if (this.frameCount == 9 )
            {
                this.active = false;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, new Vector2(position.X - (scale * source.Width) / 2, position.Y - (scale * source.Height) / 2), source, Color.White, 0, new Vector2(0, 0), scale, SpriteEffects.None, 0f);

        }
    }
}
